package com.example.jdcompetition.Service.cvsPro.jd;


import com.example.jdcompetition.Controller.UniformReponseHandler;
import com.example.jdcompetition.Entity.CallResultMsg;
import com.example.jdcompetition.Service.cvsPro.time.CollectTime;
import com.example.jdcompetition.Service.cvsPro.time.DeliverTime;
import com.example.jdcompetition.Service.cvsPro.time.TransitTime;
import com.example.jdcompetition.Entity.OnePieceIno;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

import static com.example.jdcompetition.JdcompetitionApplication.*;
import static com.example.jdcompetition.Service.cvsPro.dataprocess.CsvProcess.*;


/**
 * @author henrydai
 * @date 2021/6/7 9:43
 */
@Service
@Slf4j
public class Solution {
    @Autowired
    private UniformReponseHandler<String> uniformReponseHandler;

    @Autowired
    private ExeCollect exeCollect;
    @Autowired
    private ExeTransit exeTransit;
    @Autowired
    private ExeDeliver exeDeliver;

    //address:(0下单时间,1始发省,2始发市,3始发县,4始发镇,5目的省,6目的市,7目的县,8目的镇,9下单时间)
    public CallResultMsg inqury(OnePieceIno opi)
    {
        log.info("执行查询");
        //int oriCount = 0;   //始发地址级数
        //int destCount = 0;  //目的地址级数
        String[] ori=new String[]{"","","",""};
        String[] des=new String[]{"","","",""};
        //处理订单信息
            //0:处理一下下单时间
        String[] temp_s = opi.getOrderTime().split(" ");
            //下单日期
        long orderDate = 0;
        try {
            orderDate = solveDate(temp_s[0]);
        } catch (ParseException e) {
            log.error(opi.toString());
            log.error("处理订单日期错误");
            e.printStackTrace();
            return uniformReponseHandler.unkownException("处理订单日期错误");
        }
        //下单具体时间
        long orderTime = solveTime(temp_s[1]);

        //查询始发地址
        ori[0]=opi.getOriPro();
        ori[1]=opi.getOriCity();
        ori[2]=opi.getOriDist();
        ori[3]=opi.getOriDist();

        //查询目的地址
        des[0]=opi.getDesPro();
        des[1]=opi.getDesCity();
        des[2]=opi.getDesDist();
        des[3]=opi.getDesVill();

        //**************************执行查揽收
        log.info("计算揽收延时");
            //全查询地址
        long collectAdd = 0;
            //揽收耗费了0:1天
        collectAdd=exeCollect.execute(ori,des,orderDate,orderTime);
        if(collectAdd==-1)
        {
            log.error("揽收时延返回-1,终止查询");
            return uniformReponseHandler.collectComputeFailed("揽收失败,终止查询");
        }
        log.info(opi+"-->揽收延时:"+collectAdd);

        //**************************查中转时间
        log.info("计算中转延时");
        //中转级别2-4
        long transitAdd = 0;
            //transitTime:单纯的中转时间
        transitAdd =exeTransit.execute(ori,des,orderDate);
        if(transitAdd==-1)
        {
            log.error("中转返回-1,终止查询");
            return uniformReponseHandler.transitComputeFailed("中转失败,终止查询");
        }
        log.info(opi+"-->中转延时:"+transitAdd);
        //中转是包含发货当天,在计算到达时间时需要减去发货当天


        //**************************查派送时间
        log.info("计算派送延时");
        long arriveAdd=0;
        //  arriveAdd =exeDeliver(des,orderDate,transitAdd+collectAdd); *************
        arriveAdd =exeDeliver.execute(des,orderDate);
        if(arriveAdd==-1)
        {
            log.error("派送返回-1,终止查询");
            return uniformReponseHandler.deliverComputeFailed("派送失败,终止查询");
        }
        log.info(opi+"-->派送时延:"+arriveAdd);


        long arriveTime = collectAdd+transitAdd+arriveAdd;
        log.info(opi+"-->计算出的总配送时延:"+arriveTime);

        arriveTime = orderDate+arriveTime;


        //形成配送时效格式
        String out = arriveDate(arriveTime);
        //格式化输出
        log.info(opi+"-->订单计算结果:"+out);
        return uniformReponseHandler.sendSuccessResponse(out);
    }


    //得到订单日期
    private long solveDate(String date) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.parse(date).getTime();
    }

    //得到订单准确时间(时分) 进位问题?
    private long solveTime(String time)
    {
        String[] s = time.split(":");
        return Long.parseLong(s[0])*oneHour+Long.parseLong(s[1])*oneMinute+Long.parseLong(s[2])*oneSecond;
    }

    private String arriveDate(long arriveTime)
    {
        SimpleDateFormat outFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return outFormat.format(arriveTime-oneDay); //计算日期要去掉当天
    }

}
